/*
  +----------------------------------------------------------------------+
  | PHP Version 5                                                        |
  +----------------------------------------------------------------------+
  | Copyright (c) 1997-2007 The PHP Group                                |
  +----------------------------------------------------------------------+
  | This source file is subject to version 3.01 of the PHP license,      |
  | that is bundled with this package in the file LICENSE, and is        |
  | available through the world-wide-web at the following url:           |
  | http://www.php.net/license/3_01.txt                                  |
  | If you did not receive a copy of the PHP license and are unable to   |
  | obtain it through the world-wide-web, please send a note to          |
  | license@php.net so we can mail you a copy immediately.               |
  +----------------------------------------------------------------------+
  | Author:                                                              |
  +----------------------------------------------------------------------+
*/

/* $Id: header,v 1.16.2.1.2.1 2007/01/01 19:32:09 iliaa Exp $ */

#ifdef HAVE_CONFIG_H
#include "config.h"
#endif

#include "php.h"
#include "php_ini.h"
#include "ext/standard/info.h"
#include "ext/standard/php_smart_str.h"

#include "php_waf_framework.h"
#include "waf_view.h"

zend_class_entry * waf_view_ce;

PHP_METHOD(waf_view, __construct)
{
}

PHP_METHOD(waf_view, __destruct)
{
}

PHP_METHOD(waf_view, display)
{
    zval *tpl = NULL, *data = NULL;

    if(zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "za", &tpl, &data) == FAILURE)
    {
        return;
    }

    // get Waf_Framework::getConfig();
    zval *retConfig, * waf_framework_function_getconfig;
    MAKE_STD_ZVAL(retConfig);
    MAKE_STD_ZVAL(waf_framework_function_getconfig);
    ZVAL_STRING(waf_framework_function_getconfig, "getConfig", 1);

    call_user_function(&(waf_framework_ce)->function_table, NULL, waf_framework_function_getconfig, retConfig, 0, NULL TSRMLS_CC);

    zval **retVal;
    if(zend_hash_find(Z_ARRVAL_P(retConfig), ZEND_STRS("APP_ROOT"), (void**)&retVal) != SUCCESS)
    {
        return;
    }

    zval_ptr_dtor(&waf_framework_function_getconfig);

    // tpl
    smart_str ss_tpl = {0}; 
    char *tmpTplData = Z_STRVAL_PP(retVal);
    char *tmpTpl     = Z_STRVAL_P(tpl);

    smart_str_appends(&ss_tpl, tmpTplData);
    smart_str_appends(&ss_tpl, "/views");

    // tp1 1: have /
    if(tmpTpl[0] == '/')
    {
        smart_str_appends(&ss_tpl, tmpTpl);
    }
    // tpl 2: no have /
    else{
        //todo
    }
    
    smart_str_appends(&ss_tpl, ".php");
    smart_str_0(&ss_tpl);

    //======== parse var
    zval *function_extract, *retValExtract;
    MAKE_STD_ZVAL(function_extract);
    ZVAL_STRING(function_extract, "extract", 1);

    MAKE_STD_ZVAL(retValExtract);

    // params
    zval **params;
    params = (zval **)emalloc(sizeof(zval));
    params[0] = &data; 

    call_user_function(CG(function_table), NULL, function_extract, retValExtract, 1, params TSRMLS_CC);

    zval_ptr_dtor(&function_extract);
    zval_ptr_dtor(&retValExtract);
    efree(params);

    //======== include tpl file
    include(ss_tpl.c);

    smart_str_free(&ss_tpl); 
    zval_ptr_dtor(&retConfig);
}

zend_function_entry waf_view_methods[] = {
        PHP_ME(waf_view,  __construct,    NULL,   ZEND_ACC_PUBLIC | ZEND_ACC_CTOR)
        PHP_ME(waf_view,  __destruct,     NULL,   ZEND_ACC_PUBLIC | ZEND_ACC_DTOR)
        PHP_ME(waf_view,  display,        NULL,   ZEND_ACC_STATIC | ZEND_ACC_PUBLIC)
        {NULL, NULL, NULL}
};

WAF_STARTUP_FUNCTION(waf_view)
{
        zend_class_entry ce;

        INIT_CLASS_ENTRY(ce, "Waf_View", waf_view_methods);
        waf_view_ce = zend_register_internal_class_ex(&ce, NULL, NULL TSRMLS_CC);

        return SUCCESS;
}

/*
 * Local variables:
 * tab-width: 4
 * c-basic-offset: 4
 * End:
 * vim600: noet sw=4 ts=4 fdm=marker
 * vim<600: noet sw=4 ts=4
 */
